package com.flydust.apipulse.controller;

import com.flydust.apipulse.entity.HealthCheckResult;
import com.flydust.apipulse.repository.HealthCheckResultRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = {"http://localhost:3000", "http://127.0.0.1:3000"}, allowCredentials = "true")
public class HealthCheckResultController {

    @Autowired
    private HealthCheckResultRepository healthCheckResultRepository;

    // 获取接口的健康检查历史记录
    @GetMapping("/endpoints/{endpointId}/health-check-results")
    public List<HealthCheckResult> getHealthCheckResultsByEndpointId(@PathVariable Long endpointId) {
        return healthCheckResultRepository.findTop10ByEndpointIdOrderByCheckedAtDesc(endpointId);
    }

    // 获取最新的健康检查结果
    @GetMapping("/endpoints/{endpointId}/latest-health-check-result")
    public HealthCheckResult getLatestHealthCheckResult(@PathVariable Long endpointId) {
        List<HealthCheckResult> results = healthCheckResultRepository.findTop10ByEndpointIdOrderByCheckedAtDesc(endpointId);
        return results.isEmpty() ? null : results.get(0);
    }
}